<template>
  <div class="role-container">
    <div class="left-wrapper">
      <div
        v-for="(item, index) in roleList"
        :key="item.roleId"
        class="role-item"
        :class="{ active: index === currentIndex }"
        @click="changeRole(index, item.roleId)"
      >
        <div class="role-info">
          <svg-icon
            :icon-class="index === currentIndex ? 'user-active' : 'user'"
            class="icon"
          />
          {{ item.roleName }}
        </div>

        <div class="more">
          <el-dropdown>
            <span class="el-dropdown-link">
              <svg-icon icon-class="more" />
            </span>

            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="$router.push(`/roleEdit?id=${item.roleId}`)" v-auth="'sys:role:add_edit'">编辑角色</el-dropdown-item>
              <el-dropdown-item @click.native="delRole(item.roleId)" v-auth="'sys:role:remove'">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>

      <el-button class="addBtn" size="mini" @click="$router.push('/roleAdd')" v-auth="'sys:role:add_edit'">添加角色</el-button>
    </div>
    <div class="right-wrapper">
      <template>
        <el-tabs v-model="activeName">
          <el-tab-pane label="功能权限" name="first">
            <div class="tree-wrapper">
              <div v-for="item in treeList" :key="item.id" class="tree-item">
                <div class="tree-title">{{ item.title }}</div>

                <el-tree
                  :data="item.children"
                  :props="{ label: 'title' }"
                  show-checkbox
                  default-expand-all
                  ref="tree"
                  node-key="id"
                />
              </div>
            </div>
          </el-tab-pane>
          <el-tab-pane :label="`成员(${relateUser.length})`" name="second">
            <div class="user-wrapper">
              <el-table :data="relateUser" style="width: 100%">
                <el-table-column type="index" width="250" label="序号" />
                <el-table-column prop="name" label="员工姓名" />
                <el-table-column prop="userName" label="登录账号" />
              </el-table>
            </div>
          </el-tab-pane>
        </el-tabs>
      </template>
    </div>
  </div>
</template>

<script>
//递归的为每个children添加disabled
function addDisabled(datas) {
  datas.forEach((item) => {
    item.disabled = true;
    if (item.children) {
      addDisabled(item.children);
    }
  });
}
import { getRoleApi } from "@/api/employee";
import { delRoleApi } from "@/api/system";
import {
  getTreeListApi,
  getPermisionByIdApi,
  getUserListByRoleIdApi,
} from "@/api/system";
export default {
  name: "Role",
  data() {
    return {
      activeName: "first",
      roleList: [],
      currentIndex: 0,
      treeList: [],
      perms: [],
      params: {
        page: 1,
        pageSize: 10,
      },
      relateUser: [],
    };
  },
  async created() {
    await this.getRoleList();
    await this.getTreeList();
    await this.getUserList(this.roleList[0].roleId);
    this.getRoleDetail(this.roleList[0].roleId);
  },
  methods: {
    async delRole(roleId){

      await delRoleApi(roleId)
      this.getRoleList();
    },
    async getTreeList() {
      let res = await getTreeListApi();
      this.treeList = res.data;
      addDisabled(this.treeList);
    },
    async getRoleDetail(roleId) {
      const res = await getPermisionByIdApi(roleId);
      this.perms = res.data.perms;
      this.$refs.tree.forEach((item, index) => {
        item.setCheckedKeys(this.perms[index]);
      });
    },
    async getUserList(roleId) {
      const res = await getUserListByRoleIdApi(roleId, this.params);
      this.relateUser = res.data.rows;
      console.log(this.relateUser);
    },
    changeRole(index, id) {
      this.currentIndex = index;
      this.getRoleDetail(id);
      this.getUserList(id);
    },
    async getRoleList() {
      let res = await getRoleApi();
      this.roleList = res.data;
    },
  },
};
</script>

<style scoped lang="scss">
::v-deep .el-tree-node__label {
  color: rgba(0, 0, 0, 0.3);
}
::v-deep .el-tree-node__content label.el-checkbox.is-checked {
  span.is-checked .el-checkbox__inner {
    background-color: #0094ff;
  }
}
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  padding: 20px;
  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }
    .addBtn {
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper {
      padding: 20px;
    }
  }
}
</style>
